var app = getApp();

var common = require("../../utils/Conmon.js");
Page({
  linkToMall: function() {

    wx.switchTab({
      url: '/pages/customIndex/index'
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    popin: false,
    list: [],
    selectProductId: 0,
    selectShopID: 0,
    nodata: true,
    SearchType: "all",
    dataType: '全部收藏'
  },
  changePopin: function(e) {
    var pid = e.currentTarget.id;
    var selectShopID = e.currentTarget.dataset.shopid;
    this.setData({
      popin: !this.data.popin,
      selectProductId: pid,
      selectShopID: selectShopID
    })
  },
  gotoproduct: function() {
    this.setData({
      popin: false,
    })
    var pid = this.data.selectProductId;
    var selectShopID = this.data.selectShopID;
    wx.navigateTo({
      url: '/pages/details/details?productId=' + pid + "&shopID=" + selectShopID
    })
  },
  //清空收藏
  delAll: function(e) {
    wx.showModal({
      title: '提示',
      content: '是否清空所有收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '加载中...',
            icon: 'loading'
          })
          wx.request({
            url: app.globalData.apiurl + '/mobile/api/Collection/ClearMemberCollection',
            data: {
              custId: app.globalData.custID,
              userId: wx.getStorageSync('UserInfo').UserID
            },
            method: 'GET',
            header: {
              'X_ManuName': app.globalData.manuName
            },
            success: function(res) {
              wx.hideToast();
              if (res.data.code == 1200) {
                var page = getCurrentPages().pop()
                if (page == undefined || page == null)
                  return;
                page.onLoad()
              } else {
                wx.showModal({
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "确定",
                })
              }
            }
          })
        }
      }
    })
  },
  //删除
  del: function(e) {
    var that = this;
    that.setData({
      popin: false,
    })
    wx.showModal({
      title: '提示',
      content: '是否删除收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '加载中...',
            icon: 'loading'
          })
          var data = {
            CustId: app.globalData.custID,
            UserId: wx.getStorageSync('UserInfo').UserID,
            ProductID: that.data.selectProductId,
            UnionShopID: that.data.selectShopID
          };
          wx.request({
            url: app.globalData.apiurl + '/mobile/api/Collection/CancelCollection',
            data: JSON.stringify(data),
            header: {
              'X_ManuName': app.globalData.manuName
            },
            method: 'POST',
            success: function(res) {
              wx.hideToast();
              if (res.data.code == 1200) {
                that.setData({
                  selectProductId: 0
                })
                var page = getCurrentPages().pop()
                if (page == undefined || page == null)
                  return;
                page.onLoad()
              } else {
                wx.showModal({
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "确定",
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    common.pageInit(function() {});
    loadCollect(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  open: function() {
    let that = this
    wx.showActionSheet({
      itemList: ['全部收藏', '最近7天', '最近15天', '最近一个月'],
      success: function(res) {
        if (!res.cancel) {

          switch (res.tapIndex) {
            case 0:
              that.setData({
                dataType: '全部收藏',
                SearchType: "all"
              })
              break;
            case 1:
              that.setData({
                dataType: '最近7天',
                SearchType: "near7"
              })
              break;
            case 2:
              that.setData({
                dataType: '最近15天',
                SearchType: "near15"
              })
              break;
            case 3:
              that.setData({
                dataType: '最近一个月',
                SearchType: "near31"
              })
              break;
          }
          loadCollect(that);
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    var that = this;
    var productName = e.target.dataset.productname;
    var productimg = e.target.dataset.productimg;
    var productid = e.target.dataset.productid;
    var unionshopid = e.target.dataset.unionshopid;
    var price = e.target.dataset.price;
    var linkPath = "pages/details/details?productId=" + productid + "&shopID=" + unionshopid;
   
   
    return {
      title: productName,
      path: linkPath,
      imageUrl: productimg,
      success: function(res) {
        // 转发成功
        common.logShare(productid, productName, productimg, price, 0, linkPath);
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})

//加载数据
function loadCollect(that) {
  wx.showToast({
    title: '加载中...',
    icon: 'loading'
  })
  var data = {
    CustId: app.globalData.custID,
    UserId: wx.getStorageSync('UserInfo').UserID,
    PageIndex: 1,
    PageSize: 10,
    SearchType: that.data.SearchType,
  };
  wx.request({
    url: app.globalData.apiurl + '/mobile/api/Collection/GetUnionMemberCollectionPage',
    data: JSON.stringify(data),
    method: 'POST',
    header: {
      'X_ManuName': app.globalData.manuName
    },
    success: function(res) {
      wx.hideToast();
      if (that.data.PageIndex = 1 && res.data.content.length == 0) {
        that.setData({
          nodata: false
        });
      } else {
        that.setData({
          nodata: true
        });
      }
      if (res.data.code == 1200) {
        that.setData({
          list: res.data.content
        });
      } else {
        console.log('加载失败');
      }
    }
  })
}